package com.lizheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lizheng.constants.SystemConstants;
import com.lizheng.domain.ResponseResult;
import com.lizheng.domain.entity.Article;
import com.lizheng.domain.entity.Category;
import com.lizheng.domain.vo.ArticleDetailVo;
import com.lizheng.domain.vo.ArticleListVo;
import com.lizheng.domain.vo.PageVo;
import com.lizheng.mapper.ArticleMapper;
import com.lizheng.service.ArticleService;
import com.lizheng.domain.vo.HotArticleVo;
import com.lizheng.service.CategoryService;
import com.lizheng.utils.BeanCopyUtils;
import com.lizheng.utils.RedisCache;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private CategoryService categoryService;
    @Resource
    private RedisCache redisCache;

    @Override
    public ResponseResult hotArticleList() {
        //查询热门文章 封装成ResponseResult返回
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        //必须是正式文章
        //实际项目中都不允许直接在代码中使用字面值。都需要定义成常量来使用。这种方式有利于提高代码的可维护性。
        wrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        //按照浏览量进行排序
        wrapper.orderByDesc(Article::getViewCount);
        //最多只查询10条
        Page<Article> page = new Page(SystemConstants.CURRENT_PAGE,SystemConstants.PAGE_TOTAL_SIZE);
        page(page,wrapper);
        List<Article> articles = page.getRecords();
        List<HotArticleVo> hotArticleVos = new ArrayList<>();

        for (Article article : articles) {
            HotArticleVo vo = new HotArticleVo();
            //bean拷贝 前一个是旧值 后一个是新值
            BeanUtils.copyProperties(article,vo);
            hotArticleVos.add(vo);
        }
        return ResponseResult.okResult(hotArticleVos);
    }

    @Override
    public ResponseResult articleList(Long categoryId, Integer pageNum, Integer pageSize) {
        //查询条件
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        //如果有categoryId 查询时要和传入的值相同
        wrapper.eq(Objects.nonNull(categoryId)&&categoryId>0,Article::getCategoryId,categoryId)
        //状态是正式发布的
        .eq(Article::getStatus,SystemConstants.ARTICLE_STATUS_NORMAL)
        //对istop进行降序
        .orderByDesc(Article::getIsTop);
        //分页查询
        Page<Article> page = new Page<>(pageNum,pageSize);
        page(page,wrapper);
        List<Article> articles = page.getRecords();
        for (Article article : articles) {
            //通过categoryId查询categoryname 再把值传入到artice中
            Category category = categoryService.getById(article.getCategoryId());
            article.setCategoryName(category.getName());
        }
        //封装查询结果
        List<ArticleListVo> articleListVos = BeanCopyUtils.copyBeanList(articles, ArticleListVo.class);
        return ResponseResult.okResult(new PageVo(articleListVos,page.getTotal()));
    }

    @Override
    public ResponseResult getArticeDetail(Long id) {
        //根据id查询文章
        Article article = getById(id);
        //从redis里面获取viewcount
        Integer viewCount = redisCache.getCacheMapValue("article:viewCount", id.toString());
        article.setViewCount(viewCount.longValue());
        //转换成VO
        ArticleDetailVo articleDetailVo = BeanCopyUtils.copyBean(article, ArticleDetailVo.class);
        //根据分类id查询分类名
        Long categoryId = articleDetailVo.getCategoryId();
        Category category = categoryService.getById(categoryId);
        //判断是否查询出来分类
        if(category!=null){
            articleDetailVo.setCategoryName(category.getName());
        }
        //封装响应返回
        return ResponseResult.okResult(articleDetailVo);
    }

    @Override
    public ResponseResult updateViewCount(Long id) {
        //更新redis中对应 id的浏览量
        redisCache.incrementCacheMapValue("article:viewCountMap",id.toString(),1);
        return ResponseResult.okResult();
    }
}
